#include "orangepch.h"
#include "SceneCamera.h"
#include "Core/Core.h"
#include "Core/Log.h"
#include "glm/gtc/matrix_transform.hpp"

namespace Orange {
	SceneCamera::SceneCamera()
	{
		CalcCameraPrejectMatrix();
	}

	void SceneCamera::SetViewportSize(uint32_t width, uint32_t height)
	{
		m_aspect = (float)width / (float)height;

		CalcCameraPrejectMatrix();
	}

	void SceneCamera::SetOrthographic(float size, float cameraNear, float cameraFar)
	{
		m_Prejection = PrejectionType::Orthographic;
		m_CameraViewportSize = size;
		m_CameraNear = cameraNear;
		m_CameraFar = cameraFar;
		CalcCameraPrejectMatrix();
	}

	void SceneCamera::SetPerspection(float fov, float cameraNear, float cameraFar)
	{
		m_Prejection = PrejectionType::Perspective;
		m_CameraPerspectFov = fov;
		m_CameraPerspectNear = cameraNear;
		m_CameraPerspectFar = cameraFar;
		CalcCameraPrejectMatrix();
	}



	void SceneCamera::CalcCameraPrejectMatrix()
	{

		if (m_Prejection == PrejectionType::Perspective) {
			m_PrejectMatrix = glm::perspective(m_CameraPerspectFov,m_aspect, m_CameraPerspectNear, m_CameraPerspectFar);
		}
		else {

			float viewportLeft = (float)-0.5 * m_CameraViewportSize * m_aspect;
			float viewportRight = (float)0.5 * m_CameraViewportSize * m_aspect;
			float viewportBottom = (float)-0.5 * m_CameraViewportSize;
			float viewportTop = (float)0.5 * m_CameraViewportSize;

			m_PrejectMatrix = glm::ortho(viewportLeft, viewportRight, viewportBottom,
				viewportTop, m_CameraNear, m_CameraFar);

		}

	}

}